﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Web;
using Top.Web.Api;
using Top.Core;
using Top.Data;

namespace Top.PointStore.Business
{
    /*功能说明:商品推广操作管理类
    *版本:
    *参与人:王士锋
    *修改日期:2015.3.4
    */
    /// <summary>商品推广
    /// </summary>
    public static class ProductsExpandManager
    {
        /// <summary>根据ID得到一条记录
        /// </summary>
        /// <returns></returns>
        public static async Task<T_ProductsExpand> Get(int kid)
        {
            if (kid <= 0) return new T_ProductsExpand();
            const string sql = @"select * from T_ProductsExpand where Kid = {0} limit 1";
            using (var db = new DbTour())
            {
                var result = await db.Sql(sql, kid).FirstOrDefault<T_ProductsExpand>();
                if (result != null)
                {
                    return result;
                }
                return new T_ProductsExpand();

            }
        }


        /// <summary>增加一条数据
        /// <param name="model">商品推广实体</param>
        /// <para>返回受影响行数</para>
        /// <para>-1:商品推广id有误</para>
        /// <para>-2:商品展示位名称不能为空</para>
        /// <para>-3:推广链接不能为空</para>
        /// <para>-4:图片不能为空</para>
        /// </summary>
        public static async Task<int> Add(T_ProductsExpand model)
        {
            if (model.Kid > 0) return -1;
            if (model.F_ShowName == "") return -1;
            if (model.F_ExpandUrl == "") return -2;
            if (model.F_PicUrl == "") return -3;
            const string sql = @"insert into T_ProductsExpand (F_ExpandUrl,F_PicUrl,F_ShowName,F_Sort,F_CreatedTime,F_UpdatedTime,F_IsDelete) 
                values ({0:F_ExpandUrl},
                        {0:F_PicUrl},
                        {0:F_ShowName},
                        {0:F_Sort},
                        {0:F_CreatedTime},
                        {0:F_UpdatedTime},
                        {0:F_IsDelete});
                select LAST_INSERT_ID()";

            using (var db = new DbTour())
            {
                return await db.Sql(sql, model).ExecuteScalar<int>();
            }
        }

        /// <summary>更新一条数据
        /// <param name="model">商品推广实体</param>
        /// <para>返回受影响行数</para>
        /// <para>-1:商品推广id有误</para>
        /// <para>-2:商品展示位名称不能为空</para>
        /// <para>-3:推广链接不能为空</para>
        /// <para>-4:图片不能为空</para>
        /// </summary>
        public static async Task<int> Update(T_ProductsExpand model)
        {
            if (model.Kid <= 0) return -1;
            if (model.F_ShowName == "") return -2;
            if (model.F_ExpandUrl == "") return -3;
            if (model.F_PicUrl == "") return -4;
            const string sql = @"update T_ProductsExpand set 
                F_ExpandUrl={0:F_ExpandUrl},
                F_PicUrl={0:F_PicUrl},
                F_ShowName={0:F_ShowName},
                F_Sort={0:F_Sort},
                F_UpdatedTime={0:F_UpdatedTime}
              where Kid={0:Kid}";

            using (var db = new DbTour())
            {
                return await db.Sql(sql, model).ExecuteNonQuery();
            }
        }

        /// <summary>删除一条记录
        /// </summary>
        /// <param name="kid"></param>
        /// <returns></returns>
        public static async Task<int> Delete(int kid)
        {
            if (kid <= 0) return 0;
            const string sql = @"update T_ProductsExpand set F_DeletedTime={0:F_DeletedTime},F_IsDelete={0:F_IsDelete} where Kid = {0:Kid}";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, new { F_DeletedTime = DateTime.Now, F_IsDelete = 1, Kid = kid }).ExecuteNonQuery();
            }
        }

        /// <summary>批量删除
        /// </summary>
        /// <param name="kids"></param>
        /// <returns></returns>
        public static async Task<int> DeleteList(string kids)
        {
            if (kids == "") return 0;
            string sql = string.Format(@"update T_ProductsExpand set F_DeletedTime = now(),F_IsDelete = 1 where Kid in ({0})", kids);
            using (var db = new DbTour())
            {
                return await db.Sql(sql).ExecuteNonQuery();
            }
        }


        /// <summary>获取数据列表
        /// </summary>
        /// <returns></returns>
        public static async Task<IList<T_ProductsExpand>> GetList(Pager pager)
        {
            using (var db = new DbTour())
            {
                var query = db.Table(@"select * from T_ProductsExpand");
                query.And("F_IsDelete = 0");
                var list =await query.ToList<T_ProductsExpand>(pager.StartNumber, pager.EndNumber, "F_Sort");
                //foreach (var productsExpand in list)
                //{
                //    productsExpand.Thumbs = IOHelper.GetThumbList(productsExpand.F_PicUrl);
                //}
                return list;
            }
        }
    }
}
